package com.mrzhang.glpt.oauth.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mrzhang.common.entity.system.Menu;
import com.mrzhang.common.entity.system.SystemUser;
import com.mrzhang.glpt.oauth.mapper.MenuMapper;
import com.mrzhang.glpt.oauth.mapper.UserMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 张霄峰
 * @email 29839415@qq.com
 * @date 2020/4/7 17:14
 */
@Service
public class UserManager {
    @Resource
    private UserMapper userMapper;
    @Resource
    private MenuMapper menuMapper;

    public SystemUser findByName(String username) {
        QueryWrapper<SystemUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",username);
        SystemUser systemUser = userMapper.selectOne(queryWrapper);
        SystemUser byName = userMapper.findByName(username);
        return systemUser;
    }

    /**
     * 该方法返回的是用户权限集合以逗号拼接后的值，如user:add,user:update,user:delete
     * @param username
     * @return
     */
    public String findUserPermissions(String username) {
        List<Menu> userPermissions = menuMapper.findUserPermissions(username);
        return userPermissions.stream().map(Menu::getPerms).collect(Collectors.joining(","));
    }
}
